<?php
/*
This file is part of RhizomeCV.
Copyright ©2010 University of Reading (Dr. Karsten Øster Lundqvist <k.o.lundqvist@reading.ac.uk>), 
King's College (Margarita Perez Garcia <contact@margaperez.com>) 
and EIfLE (Marc Van Coillie <marc.van.coillie@eife-l.org>). 

RhizomeCV is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

RhizomeCV is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

RhizomeCV is extended from KiteCV.
Copyright ©2006 Les Developpements Durables <contact@ldd.fr> and
Sébastien Ducoulombier <seb@ldd.fr>.
*/

function download($file) {
	if ($fd = fopen ($file, "r")) {
		$fsize = filesize($file);
		$path_parts = pathinfo($file);
		header("Content-type: application/octet-stream");
		header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
		header("Content-length: $fsize");
		header("Cache-control: private"); //use this to open files directly
    
		while(!feof($fd)) {
			$buffer = fread($fd, 2048);
			echo $buffer;
		}
		
		fclose ($fd);
	}
}

if (array_key_exists('downloadfile', $_GET)) {
	download($_GET['downloadfile']);
}
else {
	include_once 'code/KiteCVPlugin.php';

	$wp_plugins_dir = WP_PLUGIN_URL;

	function __($a) {
		// FIXME: this is cheating
		return $a;
	}

	$old_level = error_reporting(E_ALL);
	$dir = new KiteCVDir(
		dirname(__FILE__).'/data'
	);
	$plugin = new KiteCVPlugin(
		$dir,
		$wp_plugins_dir.'/RhizomeCV/requisites',	// HTML requisites URL
		'export.php?uid=%1$s&languageCode=%2$s&format=%3$s&position=%4$s'	// exportURL
	);
	$userId = $_GET['uid'];
	$plugin->setUserIdFromExport($userId);
	$languageCode = $_GET['languageCode'];
	$plugin->setLanguageCode($languageCode);
	$plugin->openFile();
	$format = $_GET['format'];
	$position = $_GET['position'];
	$accessCode = array_key_exists('accessCode', $_POST)
		? $_POST['accessCode']
		: '';
	$plugin->replyWithExport($format, $position, $accessCode);
}

exit;

?>				
